﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Arbore;

namespace PresentationWpf.DB
{
    public class LoadeArbore : DBWorker
    {
        public void Loade(ref Arbore.Arbore  arb,string nume)
        {
            using (Database1Entities instance = EntityConnectionManager.Instance.GetNewEntityConnection)
            {
                var i=instance.Arbori.Where(m=>m.Nume==nume).FirstOrDefault();
                if(i!=null){
                    var r = instance.Noduls.Where(m => m.ID == i.Radacina).FirstOrDefault();
                    Nod nd = new Nod(r.Value);
                    nd.ID = r.ID;
                    arb.Radacina = nd;
                    nodLoder(ref  arb, ref  nd);
                }
            }
        }
            private void nodLoder(ref Arbore.Arbore arb,ref Nod nod)
            {
                using (Database1Entities instance = EntityConnectionManager.Instance.GetNewEntityConnection)
                {
                    var i = nod.ID;
                    var r = instance.Noduls.Where(m => m.NodParinte == i).Select(t=>t).ToList();//.OrderBy(v => v.numar);
                    foreach (Nodul nd in r)
                    {
                        Nod n = new Nod(nd.Value);
                        n.ID = nd.ID;
                        nod.Childrens.Add(n);
                        n.parentNode = nod;
                        nodLoder(ref arb, ref  n);
                    }
                }
 
            }
    }
}
 